package cn.javaweb.model;

import cn.javaweb.entity.User;

import java.sql.ResultSet;
import java.util.List;

public class UserModel extends BaseModel{


    public Object parseObject(ResultSet rs){

        User user = new User();
        try{
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setToken(rs.getString("token"));
            return user;
        }catch (Exception e){

        }
        return null;

    }

    public User getUserByNameAndPassword(String username, String password){

        Object[] params = {username, password};
        User user = (User) selectOne("select * from user where username=? and password=?", params);
        return user;
    }

    public User getUserByToken(String token){
        Object[] params = {token};
        User user = (User) selectOne("select * from user where token=? ", params);

        return user;
    }

    public boolean updateUserToken(User user){
        String sql = "update user set token=? where id=?";
        Object [] params = {user.getToken(), user.getId()};
        int c = update(sql, params);
        return c>0;
    }
    public boolean updateUserPassword(User user){
        String sql = "update user set password=? where id=?";
        Object [] params = {user.getPassword(), user.getId()};
        int c = update(sql, params);
        return c>0;
    }
}
